package service

import (
	"context"
	"gitee.com/crack007/goose/core/ws"
	"gitee.com/crack007/goose/log"
	"github.com/crack007/go-im/src/common/model"
	"time"
)

type serverService struct {
	stopChan chan struct{}
}

func (s *serverService) Shutdown(wsServer *ws.Server) {
	log.GetLogger().Warn("Ready to stop, wait...")
	go s.broadcast()
	<-s.stopChan
}

func (s *serverService) broadcast() {
	defer func() {
		ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
		defer cancelFunc()
		<-ctx.Done()
		s.stopChan <- struct{}{}
	}()
	BroadcastService.All(model.NewBroadcastModel("shutdown", ""))
}
